#include <string>
#include <string.h>
#include <vector>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include "pocketfft.h"

using namespace std;

vector<string> split(const string& str, const string& delim) {  
	vector<string> res;  
	if("" == str) return res;  
	//先将要切割的字符串从string类型转换为char*类型  
	char * strs = new char[str.length() + 1] ; //不要忘了  
	strcpy(strs, str.c_str());   
 
	char * d = new char[delim.length() + 1];  
	strcpy(d, delim.c_str());  
 
	char *p = strtok(strs, d);  
	while(p) {  
		string s = p; //分割得到的字符串转换为string类型  
		res.push_back(s); //存入结果数组  
		p = strtok(NULL, d);  
	}  
 
	return res;  
}


void test_bluestein_fft(){
    string aStr = "6.28354,4.43527,2.49228,-5.63742,0.20659,-2.91656,4.82672,-1.85776,3.26414,8.71292,7.57135,9.1246,1.9476,0.812746,-5.85403,-2.72664,-4.88829,-2.19756,-7.99907,7.74329,-6.86312,0.67446,-2.57459,-9.53619,9.26605,-1.51382,-6.44654,3.72624,9.94511,3.14856,2.7511,-5.39289,-2.69482,9.99216,8.25556,-6.09742,-9.11162,4.2696,3.44392,8.83915,-0.495223,6.32631,1.79597,-8.18586,-8.28982,-9.95227,-0.642183,-7.81228,-0.0455605,-6.07271,7.39854,8.01994,-8.6866,-4.79218,-7.06739,6.64483,7.9134,-8.41155,1.75619,-0.759565,-1.65382,8.68173,5.88517,0.615229,8.01886,2.6845,2.28391,5.08823,-0.232854,6.09283,8.76244,-5.71433,3.52953,3.76599,4.0248,2.54683,-7.73136,-8.80428,4.6736,3.13473,7.00264,-3.18791,-8.36374,-3.03316,7.3531,8.83123,-2.51843,2.34241,4.00272,7.45924,9.44389,-8.29123,-8.17835,-7.9241,-0.0389414,3.77124,-1.67169,5.35899,1.52024,5.13276,-5.81601,-6.66263,9.55485,-4.6224,-5.386,-5.12726,-9.60127,8.32929,-0.230505,4.29124,2.47272,0.586177,-9.07412,3.89878,-3.75158,0.359569,-0.421768,5.66976,-0.477318,7.73645,-1.44334,4.10348,8.31978,3.64193,1.9976,4.72143,-0.402422,0.265992,9.63893,9.24062,-7.8485,5.06625,2.75924,9.44225,0.316771,2.16511,7.42963,-9.07036,-4.6682,2.45928,9.62096,3.98208,-1.10467,-2.70669,-6.90463,-8.05544,5.81671,-2.12253,-9.95416,7.4965,-7.90654,-6.19049,9.72791,5.7606,-7.36139,3.73608,9.42968,-8.70154,-0.758193,2.98316,-0.842694,8.41632,8.13585,8.7994,-9.79027,-2.74551,-5.85721,7.84434,9.41585,4.06705,8.59315,-2.46513,-1.02726,-9.78989,-7.83341,7.78745,7.25248,-7.47624,4.53738";

    vector<string> a_vector = split(aStr,",");

    size_t length = a_vector.size();
    float data[2*length], odata[2*length];

    for (size_t m=0; m<length; ++m)
    {
        odata[2*m] = std::stof(a_vector[m]);
        odata[2*m + 1] = 0;
    }
    
    printf("initialize data end !\n");
    memcpy (data,odata,length*sizeof(float));

    for (size_t m=0; m<2*length; ++m)
    {

        cout << data[m] << endl;
          // printf("%.16f\n",data[m]);

    }

    printf("memcpy data end !\n");
    cfft_plan plan = make_cfft_plan (length);

    
 
    
    
    printf("make_cfft_plan end !\n");
    cfft_forward (plan, data, 1.);
     printf("cfft_forward end !\n");
    // rfft_backward (plan, data, 1./length);
    destroy_cfft_plan (plan);

    printf("destroy_cfft_plan end !\n");
    for (size_t m=0; m<length; ++m)
    {

        cout << data[m] << endl;
          // printf("%.16f\n",data[m]);

    }




}


void test_complex_fft(){

    string aStr = "857.7792035618400632301927544176578521728516,-0.0000000000000000000000000000000000000000,62.6096381626772995332430582493543624877930,403.1322377682854494196362793445587158203125,-11.0527960635667739097698358818888664245605,451.0345652846764323840034194290637969970703,1227.5295085890245445625623688101768493652344,1886.2507024341027772607048973441123962402344,-5004.6353205937239181366749107837677001953125,-837.1202863931773663352942094206809997558594,1939.8855472654922778019681572914123535156250,-2062.6998969965125070302747189998626708984375,147.8187938885712355840951204299926757812500,37.4821292206850600337020296137779951095581,-5913.0881561227897691424004733562469482421875,121.2850378656941074950736947357654571533203,4878.8877723418390814913436770439147949218750,-1290.8224111526092201529536396265029907226562,2050.1491202498896200268063694238662719726562,1006.0855642466101471654837951064109802246094,-212.0756947917458319352590478956699371337891,-980.5052290345162191442796029150485992431641,-238.1497415281737914938275935128331184387207,-1113.8996307087331842922139912843704223632812,-2605.5511389194480216247029602527618408203125,1254.1267696927263841644162312150001525878906,1668.0389492556000732292886823415756225585938,-1164.4210297912024998367996886372566223144531,511.2168813212543341251148376613855361938477,317.4264535467505652377440128475427627563477,-3.6116085035426408467174042016267776489258,781.5275104762022237991914153099060058593750,-97.9416591585606397529772948473691940307617,-1111.1333509653982218878809362649917602539062,862.5952368381803125885198824107646942138672,1612.7070288518341385497478768229484558105469,-402.8661203737234473010175861418247222900391,-397.2981002642191015183925628662109375000000,-171.6710938293089725448226090520620346069336,-522.5108084010413449504994787275791168212891,349.8559303480640210182173177599906921386719,765.8051989097139085060916841030120849609375,-1684.4029700123237489606253802776336669921875,-499.7799237538060310725995805114507675170898,387.0822523542498174720094539225101470947266,14.9429061075535223324095568386837840080261,583.2299825296670405805343762040138244628906,-1012.8608619238065102763357572257518768310547,-2780.0432956692302468582056462764739990234375,661.2997623839569314441177994012832641601562,1267.1789080381709027278702706098556518554688,1039.4824834240189375123009085655212402343750,-227.4666247120489686039945809170603752136230,28.4867352553057884279041900299489498138428,-191.3519482441027719232806703075766563415527,1746.8653254752230168378446251153945922851562,2068.0393191076263974537141621112823486328125,-2113.9258131246351695153862237930297851562500,-1387.4076122276028399937786161899566650390625,1637.8566567694606419536285102367401123046875,32.8171567275369966409925837069749832153320,1732.5000075532122991717187687754631042480469,-356.9194202863361624622484669089317321777344,3840.2430454812097195826936513185501098632812,3351.4072000936002950766123831272125244140625,-1567.9929091243470793415326625108718872070312,2583.8464776420823909575119614601135253906250,-2446.7034698387760727200657129287719726562500,-705.8294002453452549161738716065883636474609,-1135.1139769455717214441392570734024047851562,-287.3333419519108247186522930860519409179688,-2386.6921349580161404446698725223541259765625,-2641.9478790779376140562817454338073730468750,5470.0922441254106161068193614482879638671875,154.1636402599554003245430067181587219238281,-3462.0106401008347347669769078493118286132812,1330.5271391968790339888073503971099853515625,530.2807680503058236354263499379158020019531,44.5327016513100630845656269229948520660400,24.5404917049350252966632979223504662513733,-31.9729588604885179847769904881715774536133,989.4851849723437453576480038464069366455078,1175.2724005411917005403665825724601745605469,538.3513388848799650077125988900661468505859,-1085.2177771240910715278005227446556091308594,-3121.9458191928342785104177892208099365234375,480.8039274909749565267702564597129821777344,1276.2441741064885718515142798423767089843750,797.7950168201738279094570316374301910400391,-2117.9327831202399465837515890598297119140625,885.7602125812711619801120832562446594238281,3710.5696580105850443942472338676452636718750,98.4518642262759158256812952458858489990234,521.1962907016895769629627466201782226562500,-1779.3319324301974120317026972770690917968750,-644.0389533350781903209281153976917266845703,1760.1195887357798710581846535205841064453125,-2049.6010866163237551518250256776809692382812,509.6867006087831555305456276983022689819336,3846.8550782013039679441135376691818237304688,-2310.2531946056328706617932766675949096679688,-1408.9373827364152020891197025775909423828125,-382.2810187165317188373592216521501541137695,-679.7530255986647489407914690673351287841797,221.7397704033890306618559407070279121398926,-641.2861318098723586444975808262825012207031,1064.9258929087986871309112757444381713867188,-476.9880601517478453388321213424205780029297,-47.8011175075083087904204148799180984497070,56.3593624742405268079892266541719436645508,-121.3478942831535078994420473463833332061768,-278.6756030554448102520836982876062393188477,434.5529636328104743370204232633113861083984,7.7214926107639172414565109647810459136963,-359.9362988585064613289432600140571594238281,92.8867848600617662668810226023197174072266,988.4249894954937190050259232521057128906250,698.7349230649309674845426343381404876708984,988.4249894954936053181882016360759735107422,-698.7349230649315359187312424182891845703125,-359.9362988585062907986866775900125503540039,-92.8867848600619510079923202283680438995361,434.5529636328108153975335881114006042480469,-7.7214926107639030306017957627773284912109,-121.3478942831535221102967625483870506286621,278.6756030554445828784082550555467605590820,-47.8011175075083087904204148799180984497070,-56.3593624742405268079892266541719436645508,1064.9258929087991418782621622085571289062500,476.9880601517472200612246524542570114135742,221.7397704033889453967276494950056076049805,641.2861318098723586444975808262825012207031,-382.2810187165312640900083351880311965942383,679.7530255986653173749800771474838256835938,-2310.2531946056337801564950495958328247070312,1408.9373827364152020891197025775909423828125,509.6867006087820755055872723460197448730469,-3846.8550782013030584494117647409439086914062,1760.1195887357789615634828805923461914062500,2049.6010866163242098991759121417999267578125,-1779.3319324301978667790535837411880493164062,644.0389533350776218867395073175430297851562,98.4518642262754610783304087817668914794922,-521.1962907016892359024495817720890045166016,885.7602125812711619801120832562446594238281,-3710.5696580105850443942472338676452636718750,797.7950168201738279094570316374301910400391,2117.9327831202408560784533619880676269531250,480.8039274909748996833513956516981124877930,-1276.2441741064887992251897230744361877441406,-1085.2177771240917536488268524408340454101562,3121.9458191928338237630669027566909790039062,1175.2724005411917005403665825724601745605469,-538.3513388848806471287389285862445831298828,-31.9729588604877221769129391759634017944336,-989.4851849723444274786743335425853729248047,44.5327016513101199279844877310097217559814,-24.5404917049349897695265099173411726951599,1330.5271391968785792414564639329910278320312,-530.2807680503051415144000202417373657226562,154.1636402599549455771921202540397644042969,3462.0106401008329157775733619928359985351562,-2641.9478790779376140562817454338073730468750,-5470.0922441254106161068193614482879638671875,-287.3333419519110520923277363181114196777344,2386.6921349580156856973189860582351684570312,-705.8294002453454822898493148386478424072266,1135.1139769455717214441392570734024047851562,2583.8464776420819362101610749959945678710938,2446.7034698387778917094692587852478027343750,3351.4072000936002950766123831272125244140625,1567.9929091243463972205063328146934509277344,-356.9194202863361624622484669089317321777344,-3840.2430454812083553406409919261932373046875,32.8171567275370819061208749189972877502441,-1732.5000075532120717980433255434036254882812,-1387.4076122276028399937786161899566650390625,-1637.8566567694613240746548399329185485839844,2068.0393191076254879590123891830444335937500,2113.9258131246328957786317914724349975585938,-191.3519482441027719232806703075766563415527,-1746.8653254752230168378446251153945922851562,-227.4666247120488264954474288970232009887695,-28.4867352553061721209815004840493202209473,1267.1789080381706753541948273777961730957031,-1039.4824834240193922596517950296401977539062,-2780.0432956692302468582056462764739990234375,-661.2997623839576135651441290974617004394531,583.2299825296668132068589329719543457031250,1012.8608619238066239631734788417816162109375,387.0822523542497606285905931144952774047852,-14.9429061075535649649737024446949362754822,-1684.4029700123228394659236073493957519531250,499.7799237538063152896938845515251159667969,349.8559303480636231142852921038866043090820,-765.8051989097133400719030760228633880615234,-171.6710938293088304362754570320248603820801,522.5108084010417996978503651916980743408203,-402.8661203737234473010175861418247222900391,397.2981002642191015183925628662109375000000,862.5952368381799715280067175626754760742188,-1612.7070288518345932970987632870674133300781,-97.9416591585599576319509651511907577514648,1111.1333509653982218878809362649917602539062,-3.6116085035428682203928474336862564086914,-781.5275104762022237991914153099060058593750,511.2168813212545614987902808934450149536133,-317.4264535467499399601365439593791961669922,1668.0389492555996184819377958774566650390625,1164.4210297912024998367996886372566223144531,-2605.5511389194475668773520737886428833007812,-1254.1267696927272936591180041432380676269531,-238.1497415281738767589558847248554229736328,1113.8996307087329569185385480523109436035156,-212.0756947917456045615836046636104583740234,980.5052290345162191442796029150485992431641,2050.1491202498896200268063694238662719726562,-1006.0855642466101471654837951064109802246094,4878.8877723418399909860454499721527099609375,1290.8224111526089927792781963944435119628906,-5913.0881561227906786371022462844848632812500,-121.2850378656947896161000244319438934326172,147.8187938885713208492234116420149803161621,-37.4821292206848823980180895887315273284912,1939.8855472654922778019681572914123535156250,2062.6998969965143260196782648563385009765625,-5004.6353205937230086419731378555297851562500,837.1202863931767979011056013405323028564453,1227.5295085890238624415360391139984130859375,-1886.2507024341036867554066702723503112792969,-11.0527960635660917887435061857104301452637,-451.0345652846765460708411410450935363769531,62.6096381626774700634996406733989715576172,-403.1322377682856767933117225766181945800781";
    vector<string> a_vector = split(aStr,",");
 
      
    
    size_t length = a_vector.size();
    float data[length], odata[length];

    for (size_t m=0; m<length; ++m)
    {
        odata[m] = std::stof(a_vector[m]);
        // odata[2*m + 1] = 0;
    }
    
    printf("initialize data end !\n");
    memcpy (data,odata,length*sizeof(float));

    for (size_t m=0; m<length; ++m)
    {

        cout << data[m] << endl;
          // printf("%.16f\n",data[m]);

    }

    printf("memcpy data end !\n");
    cfft_plan plan = make_cfft_plan (length/2);

    
 
    
    
    printf("make_cfft_plan end !\n");
    // cfft_forward (plan, data, 1.);
    cfft_backward (plan, data, 2./length);
     printf("cfft_forward end !\n");
    // rfft_backward (plan, data, 1./length);
    destroy_cfft_plan (plan);

    printf("destroy_cfft_plan end !\n");
    for (size_t m=0; m<length; ++m)
    {

        // cout << data[m] << endl;
          printf("%.16f\n",data[m]);

    }
}

int main(int argc, char* argv[])
  {

    // test_bluestein_fft();


// for (int i = 1 ; i <= 512 ; i++)
    cfft_plan plan = make_cfft_plan (6);





    




  }